//
//  WelfareViewController.m
//  KMedicine
//
//  Created by 余伟康 on 16/7/7.
//  Copyright © 2016年 caomu. All rights reserved.
//
//  公益模块

#import "WelfareViewController.h"

@interface WelfareViewController ()<UIWebViewDelegate>

@property (nonatomic,strong) UIWebView *welfareWebView;

@end

@implementation WelfareViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    UIButton * backButton = [UIButton buttonWithType:UIButtonTypeSystem];
    backButton.frame = CGRectMake(10, 20, 20, 20);
    [backButton setBackgroundImage:[UIImage imageNamed:@"JianTou"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backButton];
    
    UIWebView *wealfareWebView = [[UIWebView alloc]init];
    self.welfareWebView = wealfareWebView;
    
    [self.view addSubview:self.welfareWebView];
    
    [self.welfareWebView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_equalTo(0);
    }];
    
    
    NSString *urlString = @"http://202.106.210.115:18080/hyde-pluto-h360/nt/appEntrance.htm";
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
    
    self.welfareWebView.delegate = self;
    
    [self.welfareWebView loadRequest:request];
    
}


//用苹果自带的返回键按钮处理如下(自定义的返回按钮)
- (void)back:(UIBarButtonItem *)btn
{
    if ([self.welfareWebView canGoBack]) {
        [self.welfareWebView goBack];
        
    }else{
        [self.view resignFirstResponder];
        [self.navigationController popViewControllerAnimated:YES];
    }
}

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    //发请求之前调用
    /*
     1.webview
     2.request->url
     3.navigationType导航类型
     返回 YES 允许执行请求
     */
    NSURL *url = request.URL;
    
    //协议
    NSString *scheme = url.scheme;
    
    //主机地址
    NSString *host = url.host;
    //路径
    NSArray<NSString *> *paths = url.pathComponents;
    
    //判断是否我的协议
    if([scheme isEqualToString:@"mysources"]){
        
        //执行方法,参数
        NSString *method = paths[1];
        NSString *param = paths[2];
        
        //创建sel对象
        SEL sel = NSSelectorFromString(method);
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
        [self performSelector:sel withObject:param];
        
        return NO;
    }
    
    return YES;
}




@end
